package com.swss.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginFilter extends HttpServlet implements Filter {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1966654311655471108L;

	private FilterConfig filterConfig;

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		// TODO Auto-generated method stub

		try {
			HttpServletRequest hreq = (HttpServletRequest) request;
			HttpServletResponse hrsp = (HttpServletResponse) response;
			HttpSession ses = hreq.getSession();
			if (ses.getAttribute("user") == null
					&& hreq.getRequestURI().indexOf("index.jsp") == -1) {
				hreq.getRequestDispatcher("/index.jsp")
						.forward(hreq, hrsp);

			} else {
				filterChain.doFilter(hreq, hrsp);
			}

		} catch (ServletException sx) {
			filterConfig.getServletContext().log(sx.getMessage());
		} catch (IOException iox) {
			filterConfig.getServletContext().log(iox.getMessage());
		}

	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
